<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderDetailsId">
        <el-input
          v-model="queryParams.orderDetailsId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceContent">
        <el-input
          v-model="queryParams.serviceContent"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价时间" prop="TheTime">
        <el-date-picker
          v-model="queryParams.TheTime"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="星级" prop="starRating">
        <el-select v-model="queryParams.starRating" placeholder="全部">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
      <el-table-column label="订单编号" align="center" prop="orderDetailsId" />
      <el-table-column label="服务内容" align="center" prop="serviceContent" />
      <el-table-column label="评价者" align="center" prop="reviewers" >
        <template slot-scope="scope">
          <span>{{ scope.row.user.userName }} </span><br>
          <span>{{ scope.row.user.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="星级" align="center" prop="starRating" >
        <template slot-scope="scope">
          <span>{{scope.row.starRating}}星</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="90" :height="90"/>
        </template>
      </el-table-column>
      <el-table-column label="评价内容" align="center" prop="evaluationContent" />
      <el-table-column label="评价时间" align="center" prop="evaluationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaluationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listEvaluation, getEvaluation, delEvaluation, addEvaluation, updateEvaluation } from "@/api/carService/evaluation";

export default {
  name: "Evaluation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评价管理表格数据
      evaluationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderDetailsId: null,
        serviceContent: null,
        reviewers: null,
        starRating: null,
        evaluationContent: null,
        evaluationTime: null,

        // 时间查询字段
        TheTime: ['',''],
        startTime: null,
        endTime: null,

      },

      // 搜索 评价 星级
      options: [{
        value: null, label: '全部'
      }, {
        value: 1, label: '1星'
      }, {
        value: 2, label: '2星'
      }, {
        value: 3, label: '3星'
      }, {
        value: 4, label: '4星'
      }, {
        value: 5, label: '5星'
      }],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评价管理列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.TheTime[0] !== '') {
        this.queryParams.startTime = this.queryParams.TheTime[0];
      }
      if(this.queryParams.TheTime[1] !== '') {
        this.queryParams.endTime = this.queryParams.TheTime[1];
      }
      listEvaluation(this.queryParams).then(response => {
        this.evaluationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderDetailsId: null,
        serviceContent: null,
        reviewers: null,
        starRating: null,
        image: null,
        evaluationContent: null,
        evaluationTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      this.queryParams.TheTime = ['',''];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderDetailsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评价管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderDetailsId = row.orderDetailsId || this.ids
      getEvaluation(orderDetailsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评价管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderDetailsId != null) {
            updateEvaluation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvaluation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderDetailsIds = row.orderDetailsId || this.ids;
      this.$modal.confirm('是否确认删除评价管理编号为"' + orderDetailsIds + '"的数据项？').then(function() {
        return delEvaluation(orderDetailsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('carService/evaluation/export', {
        ...this.queryParams
      }, `evaluation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
